package nl.ViewerServer.ServerManager;

import java.io.File;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.ArrayList;

import nl.ViewerServer.ServerManager.JobManager.ParaViewMachine;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Config {
   // Database constants
   public String                     databaseName                     = "Viewer";
   public String                     databaseHost                     = "ubuntupc";
   public int                        databasePort                     = 3306;
   public String                     databaseUser                     = "root";
   public String                     databasePassword                 = "";

   // JobManager constants
   // the maximum amount of simultaneous worker threads
   public int                        poolSize                         = 3;
   // when a worker fails, the associated session is restarted <sessionRetryCount> times
   public int                        sessionRetryCount                = 3;

   // the amount of paraview threads that may be started, this should be higher than the poolSize, otherwise some
   // workers will not run
   public int                        paraviewThreadCount              = 4;
   public int                        maxParaviewThreadCountPerWorker  = 4;

   // ParaviewInterface constants
   public File                       paraviewExecutablePath           = new File( "/home/jeroen/paraview340-build/bin" );
   public int                        paraviewExecutableStartupTimeout = 20000;                                           // in
   // milliseconds

   public ArrayList<ParaViewMachine> paraViewMachines                 = new ArrayList<ParaViewMachine>();

   public String                     MPIMachineFile                   = "mpd.hosts";

   public String                     pythonServerScript               = "python_server.py";

   // Worker constants
   // When a worker is associated with a session, the worker will close after <sessionTimeout> ms of inactivity
   public int                        sessionWorkerTimeout             = 120000;
   public int                        closedSessionRemoveTimeout       = 60000;

   // when a session is idle for this amount of time, it will be removed
   public int                        sessionTimeout                   = 600000;

   // Webservice constants
   // Some webservice calls MUST return within <RPCTimeoutMs> ms
   public int                        RPCTimeoutMs                     = 60000;

   // DataManager constants
   // The location where all results are stored
   public String                     dataStore                        = "/home/jeroen/store/";

   /**
    * Write a config to an xml file (config.xml)
    * 
    * @param config,
    *           when specified use the provided Config, when null a default config will be written
    * @throws Exception
    */
   public static void writeConfig( Config config ) throws Exception {
      XStream xstream = new XStream( new DomDriver() );
      xstream.alias( "Config", Config.class );
      xstream.alias( "ParaViewMachine", ParaViewMachine.class );
      PrintWriter pw = new PrintWriter( "config.xml" );
      if( config == null ) {
         config = new Config();
         // add a default paraview node on the localhost
         config.paraViewMachines.add( new ParaViewMachine( "localhost", 1, 1 ) );

      }

      pw.print( xstream.toXML( config ) );
      pw.close();
   }

   public static Config readConfig() throws Exception {
      XStream xstream = new XStream( new DomDriver() );
      xstream.alias( "Config", Config.class );
      xstream.alias( "ParaViewMachine", ParaViewMachine.class );
      File f = new File( "config.xml" );
      if( !f.exists() )
         writeConfig( null );

      Config c = (Config)xstream.fromXML( new FileReader( "config.xml" ) );
      return c;
   }

}
